﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public abstract class BaseLevel{
		protected abstract void InitializeCandy();
		protected abstract void InitializeCandyIce();
		public void Initialize(){
			InitializeCandy();
			InitializeCandyIce();
		}
	public abstract Vector3 GetLastPosition();
	public abstract GameObject[] GetCandy();
}
class ConcreatLevel: BaseLevel{
	private GameManager manager=GameManager.getInstance();
	private CreateCandy candy;
	private CreateIceCell cell;
	private GameObject ice_cell;
	private int rows ,cols;
	private GameObject candy_prefab;
	public ConcreatLevel (int rows,int cols){
		this.rows=rows;
		this.cols=cols;
		ice_cell=Resources.Load<GameObject>("Prefab/ice_cell");
		candy_prefab=Resources.Load<GameObject>("Prefab/Candy");
	}
	protected override void InitializeCandy(){
		candy=new CreateCandy(rows,cols,candy_prefab);
		candy.Initialize();
	}
	protected override void InitializeCandyIce (){
		cell=new CreateIceCell(ice_cell,rows,cols);
		cell.Initialize();
	}
	public override GameObject[] GetCandy(){
		return candy.GetCandy ;
	}
	public override Vector3 GetLastPosition(){
		return GetCandy()[(rows-1)*(cols-1)].gameObject.transform.position;
	}
}

class CreateCandy{
	private int rows,cols,i,j;
	private float x=0,y=3;
	private GameObject candy,obj,candy_parent;
	private GameObject[] list;
	private float pos_y=2f,bar_height=16f;
	public CreateCandy(int rows,int cols,GameObject candy){
		this.rows=rows;
		this.cols=cols;
		this.candy=candy;
		list=new GameObject[rows*cols];
		candy_parent=new GameObject("Candy Parents");
	}
	public float BarHeight{
		set{bar_height=value;}
	}
	public GameObject[] GetCandy{
		get{return list;}
	}
	public float PositionY{
		set{pos_y=value;}
	}
	public void Initialize(){
		x=(13.707019f/2)-((1.27f*cols)/2)-6.7f;
		y=-3.519728f;
		x=(13.707019f/2)-((1.27f*cols)/2)-6.7f;
		y=-3.519728f;
		for(i=0;i<rows;i++){
			for(j=0;j<cols;j++){
					obj=MonoBehaviour.Instantiate(candy,new Vector3(x,y,0),Quaternion.identity)as GameObject;
				list[(i*cols)+j]=obj;
				obj.transform.parent=candy_parent.transform;
				x+=1.5f;	
			}		
			x=(13.707019f/2)-((1.27f*cols)/2)-6.7f;
			y+=2f;
		}

	}

}

public class CreateIceCell{
	private int rows,cols,i,j;
	private GameObject cell_to_create, cell,cell_parent,wall,wall_parents;
	private float x=0,y=0;
	public CreateIceCell(GameObject cell_to_create, int rows,int cols){
		this.cell_to_create=cell_to_create;
		this.rows=rows;
		this.cols=cols;
		cell_parent=new GameObject("Ice cells");
		wall=new GameObject("Wall");
		wall.AddComponent<BoxCollider2D>().size=new Vector2(0.01f,20f);
		wall_parents=new GameObject("Walls");
	}
	public void Initialize(){
		x=(13.707019f/2)-((1.27f*cols)/2)-6.7f;
		for( i=0;i<=cols;i++){
			(MonoBehaviour.Instantiate(wall,new Vector3(x-0.78f,6,0),Quaternion.Euler(0,90,0))as GameObject).transform.parent=wall_parents.transform;
			x+=1.5f;
		}
		MonoBehaviour.Destroy(wall);
		x=(13.707019f/2)-((1.27f*cols)/2)-6.7f;
		y=-3.519728f;

			for(i=0;i<rows;i++){
				for( j=0;j<cols;j++){
					cell=MonoBehaviour.Instantiate(cell_to_create)as GameObject;
					cell.transform.position=new Vector2(x,y);
					cell.transform.localScale=new Vector3(3f,3f,0.5f);
					cell.transform.parent=cell_parent.transform;
					x+=1.5f;
				}
			x=(13.707019f/2)-((1.27f*cols)/2)-6.7f;
			y+=1.5f;						
		}
		GameObject.FindWithTag("barupper").transform.position=new Vector3(
			GameObject.FindWithTag("barupper").transform.position.x,y,0);
		GameObject.FindWithTag("settrigger").transform.position=new Vector3(
			GameObject.FindWithTag("settrigger").transform.position.x,y-0.5f,0);
		GameObject.FindWithTag("hidecandy").transform.position=new Vector3(
			GameObject.FindWithTag("hidecandy").transform.position.x,y+2.2f,0);
	}
}